/* *****************************************************************************************************
 * Copyright 2009. Dbwebx Group
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 * *****************************************************************************************************
 */

package org.dbwebx.mojo.flex.generator;

import org.granite.generator.TemplateUri;
import org.granite.generator.as3.As3TypeFactory;
import org.granite.generator.as3.DefaultAs3TypeFactory;
import org.granite.generator.as3.JavaAs3GroovyConfiguration;
import org.granite.generator.as3.JavaAs3Input;
import org.granite.generator.as3.PackageTranslator;
import org.granite.generator.as3.reflect.JavaType;
import org.granite.generator.gsp.GroovyTemplateFactory;

import java.io.File;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author Izzet_Mustafayev
 *
 */
public class AS3Configuration implements JavaAs3GroovyConfiguration {
    As3TypeFactory as3TypeFactoryImpl = new DefaultAs3TypeFactory();
    private File baseOutputDirectory;
    private File outputDirectory;
    private TemplateUri[] delegateTemplateUris = null;
    private List<PackageTranslator> translators = new ArrayList<PackageTranslator>();
    private String uid = "uid";
    private ClassLoader loader;
    private Map<String, File> classes;
    private GroovyTemplateFactory groovyTemplateFactory = null;

    public AS3Configuration(File baseOutDir, File outDir, Map<String, File> classes, ClassLoader loader) {
        baseOutputDirectory = baseOutDir;
        outputDirectory = outDir;
        this.classes = classes;
        this.loader = loader;
    }

    public As3TypeFactory getAs3TypeFactory() {
        return as3TypeFactoryImpl;
    }

    public File getBaseOutputDir(JavaAs3Input javaas3input) {
        return baseOutputDirectory;
    }

    public File getOutputDir(JavaAs3Input javaas3input) {
        return outputDirectory;
    }

    public TemplateUri[] getTemplateUris(JavaType javaType) {
        return delegateTemplateUris;
    }

    public void setupTemplateUris(String baseTemplatePath) {
        String baseTemplateUri = null;
        // Other bean templates.
        baseTemplateUri = baseTemplatePath + "/templateBase.gsp";

        String templateUri = baseTemplatePath + "/template.gsp";
        delegateTemplateUris = TemplateHelper.createTemplateUris(baseTemplateUri, templateUri);
    }

    public List<PackageTranslator> getTranslators() {
        return translators;
    }

    public String getUid() {
        return uid;
    }

    public boolean isGenerated(Class<?> clazz) {
        return classes.containsKey(clazz.getName());
    }

    public GroovyTemplateFactory getGroovyTemplateFactory() {
        if (groovyTemplateFactory == null) {
            groovyTemplateFactory = new GroovyTemplateFactory();
        }

        return groovyTemplateFactory;
    }

    public ClassLoader getClassLoader() {
        return loader;
    }
}
